<?php
require_once "config.php";

//禁止报错
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//获取ip
$ip = null;
if (getenv("HTTP_CLIENT_IP")) {
    $ip = getenv("HTTP_CLIENT_IP");
} else {
    if (getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } else {
        if (getenv("REMOTE_ADDR")) {
            $ip = getenv("REMOTE_ADDR");
        }
    }
}

//获取访问用户的浏览器的信息
function determinebrowser($Agent)
{
    if (ereg('MSIE ([0-9].[0-9]{1,2})', $Agent, $version)) {
        $browserversion = $version[1];
        $browseragent = "Internet Explorer";
    } else if (ereg('Opera/([0-9]{1,2}.[0-9]{1,2})', $Agent, $version)) {
        $browserversion = $version[1];
        $browseragent = "Opera";
    } else if (ereg('Firefox/([0-9.]{1,5})', $Agent, $version)) {
        $browserversion = $version[1];
        $browseragent = "Firefox";
    } else if (ereg('Chrome/([0-9.]{1,3})', $Agent, $version)) {
        $browserversion = $version[1];
        $browseragent = "Chrome";
    } else if (ereg('Safari/([0-9.]{1,3})', $Agent, $version)) {
        $browseragent = "Safari";
        $browserversion = "";
    } else {
        $browserversion = "";
        $browseragent = "Unknown";
    }
    return $browseragent . " " . $browserversion;
}

// 同理获取访问用户的浏览器的信息
function determineplatform($Agent)
{
    $browserplatform = '';
    if (eregi('win', $Agent) && strpos($Agent, '95')) {
        $browserplatform = "Windows 95";
    } elseif (eregi('win 9x', $Agent) && strpos($Agent, '4.90')) {
        $browserplatform = "Windows ME";
    } elseif (eregi('win', $Agent) && ereg('98', $Agent)) {
        $browserplatform = "Windows 98";
    } elseif (eregi('win', $Agent) && eregi('nt 5.0', $Agent)) {
        $browserplatform = "Windows 2000";
    } elseif (eregi('win', $Agent) && eregi('nt 5.1', $Agent)) {
        $browserplatform = "Windows XP";
    } elseif (eregi('win', $Agent) && eregi('nt 6.0', $Agent)) {
        $browserplatform = "Windows Vista";
    } elseif (eregi('win', $Agent) && eregi('nt 6.1', $Agent)) {
        $browserplatform = "Windows 7";
    } elseif (eregi('win', $Agent) && ereg('32', $Agent)) {
        $browserplatform = "Windows 32";
    } elseif (eregi('win', $Agent) && eregi('nt', $Agent)) {
        $browserplatform = "Windows NT";
    } elseif (eregi('Mac OS', $Agent)) {
        $browserplatform = "Mac OS";
    } elseif (eregi('linux', $Agent)) {
        $browserplatform = "Linux";
    } elseif (eregi('unix', $Agent)) {
        $browserplatform = "Unix";
    } elseif (eregi('sun', $Agent) && eregi('os', $Agent)) {
        $browserplatform = "SunOS";
    } elseif (eregi('ibm', $Agent) && eregi('os', $Agent)) {
        $browserplatform = "IBM OS/2";
    } elseif (eregi('Mac', $Agent) && eregi('PC', $Agent)) {
        $browserplatform = "Macintosh";
    } elseif (eregi('PowerPC', $Agent)) {
        $browserplatform = "PowerPC";
    } elseif (eregi('AIX', $Agent)) {
        $browserplatform = "AIX";
    } elseif (eregi('HPUX', $Agent)) {
        $browserplatform = "HPUX";
    } elseif (eregi('NetBSD', $Agent)) {
        $browserplatform = "NetBSD";
    } elseif (eregi('BSD', $Agent)) {
        $browserplatform = "BSD";
    } elseif (ereg('OSF1', $Agent)) {
        $browserplatform = "OSF1";
    } elseif (ereg('IRIX', $Agent)) {
        $browserplatform = "IRIX";
    } elseif (eregi('FreeBSD', $Agent)) {
        $browserplatform = "FreeBSD";
    }
    if ($browserplatform == '') {
        $browserplatform = "Unknown";
    }
    return $browserplatform;
}


$Agent = $_SERVER['HTTP_USER_AGENT'];

//获取访问用户的浏览器信息
$browser = determinebrowser($Agent);

//获取访问用户的操作系统平台
$platform = determineplatform($Agent);

//获取来源
$sourceurl = $_SERVER['HTTP_REFERER'];

//建立与mysql的连接
$mysqli = new mysqli(db_server, db_username, db_password, db_datebasename);
if ($mysqli->connect_errno) {
    die('连接数据库出错：' . $mysqli->connect_error);
}

//设置默认编码
$mysqli->set_charset('utf8');

//判断id
$id = $_GET['id'];
if (empty($id)) {
    die();
}

//生成随机数
$a = rand(1, 2);
//echo $a;

//查询记录
$result = $mysqli->query("SELECT * FROM api.file WHERE id='{$id}'");
$row = $result->fetch_assoc();
$b = "address{$a}_url";
$url = $row[$b];

//echo $url;

//更新记录
$sql[1] = "UPDATE api.file SET file.address1_num = file.address1_num+1 WHERE id = '{$id}'";
$sql[2] = "UPDATE api.file SET file.address2_num = file.address2_num+1 WHERE id = '{$id}'";

$mysqli->query($sql[$a]);

//指定时区
date_default_timezone_set("Asia/Shanghai");
$time = date('y-m-d H:i:s', time());

//插入记录
$sql = "INSERT INTO api.filelog(fileid,fileurl,browser,platform,sourceurl,ip,date)VALUES(
'{$id}',
'{$a}',
'{$browser}',
'{$platform}',
'{$sourceurl}',
'{$ip}',
'{$time}'
)";

$mysqli->query($sql);

?>
<p style="font-size:200%">这是我写的统计下载个数的小程序，<a target="_blank"
                                             href="https://github.com/taoting1234/api/blob/master/download.php">点击查看源码</a>
</p>

<script language='javascript' type='text/javascript'>
    window.location.href = '<?php echo $url; ?>'
</script>






